#include "pch.h"
#include "Typedef.h"

void DirPath::LoadKey(const char * path, const char * section, HWND hdlg, INIEdit * edit, int item_count)
{
	char buffer[MAX_PATH] = { 0 };
	int hex_val = 0;
	for (int i = 0; i < item_count; i++)
	{
		if (edit[i].idc == NULL && edit[i].type == NULL && edit[i].var == NULL && edit[i].var == NULL) break;
		GetPrivateProfileStringA(section, edit[i].name, NULL, buffer, MAX_PATH, path);
		switch (edit[i].type)
		{
			//case DLGTYPE_CHECK_BOOL:
			//	*((int *)(edit[i].var)) = atoi(buffer);
			//	if (hdlg == NULL) break;
			//	SendDlgItemMessage(hdlg, edit[i].idc, BM_SETCHECK, atoi(buffer) ? BST_CHECKED : BST_UNCHECKED, 0);
			//	break;

		case DLGTYPE_INPUT_INT:
			*((int *)(edit[i].var)) = atoi(buffer);
			if (hdlg == NULL) break;
			SetDlgItemTextA(hdlg, edit[i].idc, buffer);
			break;

		case DLGTYPE_INPUT_FLOAT:
			*((float *)(edit[i].var)) = (float)atof(buffer);
			if (hdlg == NULL) break;
			SetDlgItemTextA(hdlg, edit[i].idc, buffer);
			break;

			//case DLGTYPE_INPUT_DOUBLE:
			//	*((double *)(edit[i].var)) = atof(buffer);
			//	if (hdlg == NULL) break;
			//	SetDlgItemTextA(hdlg, edit[i].idc, buffer);
			//	break;

		case DLGTYPE_INPUT_STRING:
			strcpy((char *)(edit[i].var), buffer);
			if (hdlg == NULL) break;
			SetDlgItemTextA(hdlg, edit[i].idc, buffer);
			break;
		}
	}
}

void DirPath::SaveKey(const char * path, const char * section, HWND hdlg, INIEdit * edit, int item_count)
{
	char buffer[MAX_PATH] = { 0 };
	BOOL is_checked = FALSE;
	char hex[20] = { 0 };
	for (int i = 0; i < item_count; i++)
	{
		if (edit[i].idc == NULL && edit[i].type == NULL && edit[i].var == NULL && edit[i].var == NULL) break;

		switch (edit[i].type)
		{

		case DLGTYPE_INPUT_INT:
			if (edit[i].idc != NULL) {
				GetDlgItemTextA(hdlg, edit[i].idc, buffer, MAX_PATH);
				*((int *)(edit[i].var)) = atoi(buffer);
				WritePrivateProfileStringA(section, edit[i].name, buffer, path);
			}
			break;

		case DLGTYPE_INPUT_FLOAT:
			if (edit[i].idc != NULL) {
				GetDlgItemTextA(hdlg, edit[i].idc, buffer, MAX_PATH);
				*((float *)(edit[i].var)) = (float)atof(buffer);
				WritePrivateProfileStringA(section, edit[i].name, buffer, path);
			}

			break;

		case DLGTYPE_INPUT_STRING:
			if (edit[i].idc != NULL) {
				GetDlgItemTextA(hdlg, edit[i].idc, buffer, MAX_PATH);
				strcpy((char *)(edit[i].var), buffer);
				WritePrivateProfileStringA(section, edit[i].name, buffer, path);
			}
			break;
		}
	}
}

void DirPath::GetCurrentDir(char * path)
{
	if (path == NULL) return;
	GetModuleFileNameA(NULL, path, MAX_PATH);
	char *p = strrchr(path, '\\');
	*p = '\0';
}

BOOL DirPath::SearchFile(const char * path, const char * name)
{
	WIN32_FIND_DATAA wfd;

	char search_path[MAX_PATH] = { 0 };
	sprintf(search_path, "%s\\*.*", path);
	HANDLE search = FindFirstFileA(search_path, &wfd);

	char search_name[MAX_PATH] = { 0 };
	strcpy(search_name, name);

	BOOL finding = TRUE;
	while (finding) {
		if (wfd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) {
			int n = strlen(wfd.cFileName);
			for (int i = 0; i < n; i++) {
				wfd.cFileName[i] = (char)toupper((int)wfd.cFileName[i]);
			}

			n = strlen(search_name);
			for (int i = 0; i < n; i++) {
				search_name[i] = (char)toupper((int)search_name[i]);
			}
			//uppercase(wfd.cFileName);
			//uppercase(search_name);
			if (!strcmp(wfd.cFileName, search_name)) {
				FindClose(search);
				return TRUE;
			}
		}
		finding = FindNextFileA(search, &wfd);
	}

	FindClose(search);
	return 0;
}
